#!/bin/bash
# Edge-TTS 本地服务启动脚本（Linux/Mac）

echo "================================"
echo "Edge-TTS 本地服务启动"
echo "================================"
echo

# 检查 Python 是否安装
if ! command -v python3 &> /dev/null; then
    echo "[错误] 未检测到 Python3，请先安装 Python 3.7+"
    exit 1
fi

echo "[1/3] 检查依赖..."

# 检查并安装 edge-tts
if ! python3 -c "import edge_tts" &> /dev/null; then
    echo "[安装] edge-tts 未安装，正在安装..."
    pip3 install edge-tts flask -i https://pypi.tuna.tsinghua.edu.cn/simple
fi

# 检查并安装 flask
if ! python3 -c "import flask" &> /dev/null; then
    echo "[安装] flask 未安装，正在安装..."
    pip3 install flask -i https://pypi.tuna.tsinghua.edu.cn/simple
fi

# 检查并安装 flask-cors
if ! python3 -c "import flask_cors" &> /dev/null; then
    echo "[安装] flask-cors 未安装，正在安装..."
    pip3 install flask-cors -i https://pypi.tuna.tsinghua.edu.cn/simple
fi

echo "[2/3] 依赖检查完成"
echo "[3/3] 启动服务..."
echo

# 获取脚本所在目录
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
SCRIPT_PATH="$SCRIPT_DIR/edge_tts_server.py"

# 检查 Python 脚本是否存在
if [ ! -f "$SCRIPT_PATH" ]; then
    echo "[错误] 找不到 edge_tts_server.py 文件"
    echo "脚本路径: $SCRIPT_PATH"
    echo "当前目录: $SCRIPT_DIR"
    echo
    echo "请确保以下文件存在："
    echo "- edge_tts_server.py"
    echo "- start_edge_tts.sh"
    echo "这两个文件应该在同一目录下"
    exit 1
fi

echo "[信息] Python 脚本路径: $SCRIPT_PATH"
echo

# 启动服务
python3 "$SCRIPT_PATH"

